package BinaryTree;

import java.util.*;

class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val) {
        this.val=val;
    }
}

public class BinaryTree {

    public TreeNode createTree() {
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');

        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
//        E.right=H;

        return A;
    }


//    根据输入的字符串创建二叉树
    public static int i=0;
    public TreeNode createTree(String str) {
        TreeNode root=null;
        if(str.charAt(i)!='#') {
            root=new TreeNode(str.charAt(i));
            i++;
            root.left=createTree(str);
            root.right=createTree(str);
        }else {
            i++;
        }
        return root;
    }


//    无返回值版层序遍历
    public void levelOrder(TreeNode root) {
        if(root==null) return;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null) queue.offer(cur.left);
            if(cur.right!=null) queue.offer(cur.right);
        }
        System.out.println();
    }

//    返回二维数组版层序遍历
    /*
    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> ret=new ArrayList<>();
        if(root==null) return ret;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            int size=queue.size();
            List<Integer> list=new ArrayList<>();
            while(size>0) {
                TreeNode cur=queue.poll();
                list.add(cur.val);
                if(cur.left!=null) queue.offer(cur.left);
                if(cur.right!=null) queue.offer(cur.right);
                size--;
            }
            ret.add(list);
        }
        return ret;
    }*/


//    判断一棵树是否是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if(root==null) return true;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        TreeNode cur=root;
        while(cur!=null) {
            cur=queue.poll();
            queue.offer(cur.left);
            queue.offer(cur.right);
            cur=queue.peek();
        }
        cur=queue.poll();
        while(!queue.isEmpty()) {
            TreeNode node=queue.peek();
            if(node!=null) return false;
            queue.poll();
        }
        return true;
    }

//    求一棵树的最近公共祖先
    public TreeNode lowestAncestor(TreeNode root,TreeNode p,TreeNode q) {
        if(root==null) return null;
        if(p==root || q==root) return root;

        TreeNode leftTree=lowestAncestor(root.left,p,q);
        TreeNode rightTree=lowestAncestor(root.right,p,q);

        if(leftTree!=null && rightTree!=null) return root;
        else if(leftTree!=null) return leftTree;
        else return rightTree;
    }

//    第二种解法（链表求交点）
    public TreeNode lowestAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;

        Stack<TreeNode> stackP=new Stack();
        getPath(root,p,stackP);
        Stack<TreeNode> stackQ=new Stack();
        getPath(root,q,stackQ);

        int sizeP=stackP.size();
        int sizeQ=stackQ.size();

        if(sizeP>sizeQ) {
            int size=sizeP-sizeQ;
            while(size>0) {
                stackP.pop();
                size--;
            }
        }else {
            int size=sizeQ-sizeP;
            while(size>0) {
                stackQ.pop();
                size--;
            }
        }

        while(!stackP.isEmpty()) {
            if(stackP.peek()==stackQ.peek()) {
                return stackP.peek();
            }else {
                stackP.pop();
                stackQ.pop();
            }
        }
        return null;
    }
    public boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack) {
        if(root==null) return false;
        stack.push(root);
        if(root==node) return true;

        if(!getPath(root.left,node,stack) &&
                !getPath(root.right,node,stack)) {
            stack.pop();
            return false;
        }
        return true;
    }

//    递归前序遍历
    public void preOrder(TreeNode root) {
        if(root==null) return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }


//    非递归前序遍历
    public void preOrderNot(TreeNode root) {
        if(root==null)return;
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null || !stack.isEmpty()) {
            while(cur!=null) {
                stack.push(cur);
                System.out.print(cur.val+" ");
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            cur=top.right;
        }
    }

    public void inOrder(TreeNode root) {
        if(root==null) return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

//    非递归中序遍历
    public void inOrderNot(TreeNode root) {
        Stack<TreeNode> stack =new Stack<>();
        TreeNode cur=root;
        while(cur!=null || !stack.isEmpty()) {
            while(cur!=null) {
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            System.out.print(top.val+" ");
            cur=top.right;
        }
    }


    public void postOrder(TreeNode root) {
        if(root==null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

//    非递归后序遍历
    public void postOrderNot(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode prev=null;
        while(cur!=null || !stack.isEmpty()) {
            while(cur!=null) {
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.peek();
            if(top.right==null || top.right==prev) {
                stack.pop();
                System.out.print(top.val+" ");
                prev=top;
            }else {
                cur=top.right;
            }
        }
    }

//    7.31的根据前序，中序创建二叉树没有写

    //根据前序，中序创建二叉树
    /*
        preIndex标记前序数组遍历到的位置
        rootIndex表示中序数组中找到的根节点的位置
    */
    /*

    public int preIndex;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder,inorder,0,inorder.length-1);
    }

    private TreeNode buildTreeChild(int[] preorder,int[] inorder,int inBegin,int inEnd) {
        if(inBegin>inEnd) return null;
        TreeNode root=new TreeNode(preorder[preIndex]);
        int rootIndex=findRootIndex(inorder,inBegin,inEnd,preorder[preIndex]);
        preIndex++;
        root.left=buildTreeChild(preorder,inorder,inBegin,rootIndex-1);
        root.right=buildTreeChild(preorder,inorder,rootIndex+1,inEnd);
        return root;
    }

    private int findRootIndex(int[] inorder,int inBegin,int inEnd,int key) {
        for(int i=inBegin;i<=inEnd;i++) {
            if(inorder[i]==key)return i;
        }
        return -1;
    }

     */


    //根据后序，中序创建二叉树
    /*

    public int postIndex=0;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        postIndex=postorder.length-1;
        return buildTreeChild(inorder,postorder,0,inorder.length-1);
    }

    private TreeNode buildTreeChild(int[] inorder,int[] postorder,int inBegin,int inEnd) {
        if(inBegin>inEnd)return null;
        TreeNode root=new TreeNode(postorder[postIndex]);
        int rootIndex=findRootIndex(inorder,inBegin,inEnd,postorder[postIndex]);
        postIndex--;
        root.right=buildTreeChild(inorder,postorder,rootIndex+1,inEnd);
        root.left=buildTreeChild(inorder,postorder,inBegin,rootIndex-1);
        return root;
    }

    private int findRootIndex(int[] inorder,int inBegin,int inEnd,int key) {
        for(int i=inBegin;i<=inEnd;i++) {
            if(inorder[i]==key)return i;
        }
        return -1;
    }

     */


//    根据二叉树创建字符串
    public String tree2str(TreeNode root) {
        StringBuilder stringBuilder=new StringBuilder();
        tree2strChild(root,stringBuilder);
        return stringBuilder.toString();
    }

    private void tree2strChild(TreeNode root,StringBuilder stringBuilder) {
        if(root==null) return;
        stringBuilder.append(root.val);

        if(root.left!=null) {
            stringBuilder.append("(");
            tree2strChild(root.left,stringBuilder);
            stringBuilder.append(")");
        }else {
            if(root.right==null) {
                return;
            }else {
                stringBuilder.append("()");
            }
        }

        if(root.right==null) {
            return;
        }else {
            stringBuilder.append("(");
            tree2strChild(root.right,stringBuilder);
            stringBuilder.append(")");
        }
    }

}
